package me.hl.common.pool;

import me.hl.reflect.Person;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by haoliang on 2016/8/5.
 */
public class PoolTest {

    private static Logger logger = LoggerFactory.getLogger(PoolTest.class);

    private static final PoolableObjectFactory factory = new TestPoolableFactory();
    private static final GenericObjectPool pool = new GenericObjectPool(factory);

    @Test
    public void test() throws Exception {
        pool.setMaxActive(3);
        pool.setMaxWait(3000);
        //pool.setWhenExhaustedAction((byte)0);
        pool.setMinEvictableIdleTimeMillis(3000);// 设定在进行后台对象清理时，休眠时间超过了3000毫秒的对象为过期
        for (int i = 0; i < 10; i++) {
            logger.info("=================={}==================", i);
            logger.info("池中处于闲置状态的实例pool.getNumIdle():{}", pool.getNumIdle());
            Person person = (Person) pool.borrowObject();
            logger.info("池中所有在用实例数量pool.getNumActive():{}", pool.getNumActive());
            //pool.returnObject(person);

        }


    }

}
